package com.wanglibao.woodtrade.woodtrade.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by sony on 2016/5/26.
 */
public class ConserveImage {
//    @SuppressLint("SimpleDateFormat")
//    public static String ConserveImageMethod(Context context, String imagePath, String imageName , CarInfo carInfo , CarImage carImage, int requestCode){
//        String newFilePath = carInfo.mLocalImagePath + carImage.mLocationCode + ".jpg";
//        if (requestCode == Content.mPictureRequestCode) {
//            String imageTemporaryPath = Content.CAR_IMAGE_PATH + Content.CAR_IMAGE_NAME;
//            if(!new File(imageTemporaryPath).exists()){
////                ToastUtils.(context, "选择的图片不存在，请选择其他图片");
//                ToastUtil.createNormalToast("选择的图片不存在，请选择其他图片");
//                return ""; //图片的不存在
//            }else if (!isWidthGtHeight(imageTemporaryPath)) {
//                ToastUtils.show(context, "图片必须是横拍");
//                return ""; //图片的宽度必须必高度大
//            }
//            compressImage(imageTemporaryPath, newFilePath
//                    , Bitmap.CompressFormat.JPEG, 100);
//        }else if (requestCode == Content.mGalleryRequestCode) {
//            if(!new File(imagePath + imageName).exists()){
//                ToastUtils.show(context, "选择的图片不存在，请选择其他图片");
//                return ""; //图片的不存在
//            }else if (!isWidthGtHeight(imagePath + imageName)){
//                ToastUtils.show(context, "图片必须是横拍");
//                return "";
//            } //图片的宽度必须必高度大
//            compressImage(imagePath + imageName, newFilePath
//                    , Bitmap.CompressFormat.JPEG, 100);
//        }
//        return newFilePath;
//    }

    private static boolean isWidthGtHeight(String filePath){
        BitmapFactory.Options opts=new BitmapFactory.Options();
        opts.inJustDecodeBounds=true;
        BitmapFactory.decodeFile(filePath, opts);
        int height=opts.outHeight;
        int width=opts.outWidth;
        return width > height ? true : false ;
    }

    public static File compressImage(String imageFilePath,String newFilePath,String fileName,Bitmap.CompressFormat fileType,int quality){
        FileOutputStream fos = null;
        try {
            File dir = new File(newFilePath);
            if (!dir.exists()) dir.mkdirs();
            File f = new File(dir, fileName);
            fos = new FileOutputStream(f);
            BitmapFactory.Options opts=new BitmapFactory.Options();
            opts.inJustDecodeBounds=true;
            BitmapFactory.decodeFile(imageFilePath, opts);
            int height=opts.outHeight;
            int width=opts.outWidth;
            int inSampleSize=1;
            if (height>600||width>800) {
                inSampleSize=Math.round((float)width/(float)800)+1;
            }
            opts.inJustDecodeBounds=false;
            opts.inSampleSize=inSampleSize;

            Bitmap bitmap= BitmapFactory.decodeFile(imageFilePath, opts);
            //获取图片的旋转角度
            int angle=getExifOrientation(imageFilePath);
            System.out.println("图片的旋转角度"+angle);
            if (angle != 0) {
                // 下面的方法主要作用是把图片转一个角度，也可以放大缩小等
                Matrix m = new Matrix();
                int width1 = bitmap.getWidth();
                int height1 = bitmap.getHeight();
                m.setRotate(angle); // 旋转angle度
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, width1, height1,
                        m, true);// 重新生成图片
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int options = quality;
            bitmap.compress(fileType, options, baos);
            while (baos.toByteArray().length / 1024 > 100) {
                baos.reset();
                options -= 10;
                bitmap.compress(fileType, options, baos);
            }
            bitmap.recycle();
            bitmap=null;
            try {
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
                return f;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }
    public static int getExifOrientation(String filepath) {
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch(orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
            }
        }
        return degree;
    }
}
